import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.Rectangle;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

public class Statistics extends JScrollPane {

	public Statistics() {
		JPanel statisticsPanel = new JPanel();
		
		statisticsPanel.setLayout(new GridLayout(2, 3));
		
		statisticsPanel.add(createJPanel1());
		statisticsPanel.add(createJPanel2());
		statisticsPanel.add(createJPanel3());
		statisticsPanel.add(createJPanel4());
		statisticsPanel.add(createJPanel5());
		statisticsPanel.add(createJPanel6());

		this.setViewportView(statisticsPanel);
	}

	private JPanel createJPanel1() {
		JPanel panel1 = new JPanel();
		panel1.setAlignmentX(LEFT_ALIGNMENT);
		panel1.setLayout(new BoxLayout(panel1, BoxLayout.X_AXIS));
		panel1.setBorder(BorderFactory.createLineBorder(Color.black));
		panel1.add(new PieChart());

		return panel1; 
	}

	private JPanel createJPanel2() {
		JPanel panel2 = new JPanel();
		panel2.setAlignmentX(LEFT_ALIGNMENT);
		panel2.setLayout(new BoxLayout(panel2, BoxLayout.X_AXIS));
		panel2.setBorder(BorderFactory.createLineBorder(Color.black));
		panel2.add(new PieChart());

		return panel2;
	}

	private JPanel createJPanel3() {
		JPanel panel3 = new JPanel();
		panel3.setAlignmentX(LEFT_ALIGNMENT);
		panel3.setLayout(new BoxLayout(panel3, BoxLayout.X_AXIS));
		panel3.setBorder(BorderFactory.createLineBorder(Color.black));
		panel3.add(new PieChart());

		return panel3;
	}

	private JPanel createJPanel4() {
		JPanel panel4 = new JPanel();
		panel4.setAlignmentX(LEFT_ALIGNMENT);
		panel4.setLayout(new BoxLayout(panel4, BoxLayout.X_AXIS));
		panel4.setBorder(BorderFactory.createLineBorder(Color.black));
		panel4.add(new PieChart());

		return panel4;
	}

	private JPanel createJPanel5() {
		JPanel panel5 = new JPanel();
		panel5.setAlignmentX(LEFT_ALIGNMENT);
		panel5.setLayout(new BoxLayout(panel5, BoxLayout.X_AXIS));
		panel5.setBorder(BorderFactory.createLineBorder(Color.black));
		panel5.add(new PieChart());

		return panel5;
	}

	private JPanel createJPanel6() {
		JPanel panel6 = new JPanel();
		panel6.setLayout(new BoxLayout(panel6, BoxLayout.X_AXIS));
		panel6.setAlignmentX(LEFT_ALIGNMENT);
		panel6.setBorder(BorderFactory.createLineBorder(Color.black));
		panel6.add(new PieChart());

		return panel6;
	}

	static class Slice {
		double value;
		Color color;

		public Slice(double value, Color color) {
			this.value = value;
			this.color = color;
		}
	}

	static class PieChart extends JComponent {
		Slice[] slices = { new Slice(5, Color.white), new Slice(33, Color.red),
				new Slice(20, Color.black), new Slice(32, Color.blue) };

		PieChart() {
		}

		public void paint(Graphics g) {
			drawPie((Graphics2D) g, getBounds(), slices);
		}

		void drawPie(Graphics2D g, Rectangle area, Slice[] slices) {
			double total = 0.0D;
			for (int i = 0; i < slices.length; i++) {
				total += slices[i].value;
			}
			double curValue = 0.0D;
			int startAngle = 0;
			for (int i = 0; i < slices.length; i++) {
				startAngle = (int) (curValue * 360 / total);
				int arcAngle = (int) (slices[i].value * 360 / total);
				g.setColor(slices[i].color);
				g.fillArc(area.x, area.y, area.width, area.height, startAngle,
						arcAngle);
				curValue += slices[i].value;
			}
		}

	}

	public static void main(String[] args) {
		Statistics mainframe = new Statistics();
		JFrame frame = new JFrame();
		frame.getContentPane().add(mainframe);

		frame.setSize(new Dimension(1100, 650));
		frame.setLocationRelativeTo(null);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		frame.setVisible(true);
	}
}